package com.parkreport.admin.user.service;

import java.util.List;

import com.common.base.BaseService;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.pager.PageHelper;
import com.common.util.StringUtil;
import com.parkreport.vo.Roles;
import com.parkreport.vo.UsersVO;

public class UserService extends BaseService {

	
	public List find(UsersVO vo,PageHelper pager){
		StringBuilder sql = new StringBuilder("select * from USERS u  where 1=1 and u.DEL_FLAG='0' and u.id<>'0' ");
		if(vo!=null){
			if(!StringUtil.isBlank(vo.getUserFullname())){
				sql.append(" and u.USER_FULLNAME like '%"+vo.getUserFullname()+"%'");
			}
			if(!StringUtil.isBlank(vo.getUsername())){
				sql.append(" and u.USERNAME like '%"+vo.getUsername()+"%'");
			}
			if(!StringUtil.isBlank(vo.getRoleId())){
				sql.append(" and u.ROLE_ID = '"+vo.getRoleId()+"'");
			}
		}
		List<UsersVO> list = DB4ObjUtil.executePager4Obj(new UsersVO(), sql.toString(),pager);
		
		return list;
	}
	
	public UsersVO get(String id){
		String sql = "select * from USERS where 1=1 and ID=?";
		List<UsersVO> list = DB4ObjUtil.execute4Obj(new UsersVO(), sql, id);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
	public void updatPass(String id,String password){
		String sql = "update USERS set PASSWORD=? where id=?";
		if(!StringUtil.isBlank(id)){
			DBBaseUtil.executeUpdate(sql,password, id);
		}
		
	}
	public String isApply(String username,String id){
		String sql = "select * from USERS where 1=1 and USERNAME=? and ID<>?";
		List<UsersVO> list = DB4ObjUtil.execute4Obj(new UsersVO(), sql, username,id);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public String isApply(UsersVO vo){
		String sql = "select * from USERS where 1=1 and USERNAME=? and ID<>?";
		List<UsersVO> list = DB4ObjUtil.execute4Obj(new UsersVO(), sql, vo.getUsername(),vo.getId());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public void delete(String ids){
		//String sql = "delete from USERS where id=?";
		String sql = "update USERS set DEL_FLAG='1' where id=?";
		if(!StringUtil.isBlank(ids)){
			String[] idArr = ids.split(",");
			DBBaseUtil.executeBatchUpdate(sql, idArr);
			
		}
	}
	
	public void delete(String[] ids){
		//String sql = "delete from USERS where id=?";
		String sql = "update USERS set DEL_FLAG='1' where id=?";
		if(!StringUtil.isBlank(ids)){
			DBBaseUtil.executeBatchUpdate(sql, ids);
			
		}
		
		
	}
	public String save(UsersVO vo){
		if(vo!=null){
			String isHave = isApply(vo);
			if("0".equals(isHave)){
				if(StringUtil.isBlank(vo.getId())){
						String id = StringUtil.getUuid();
						String sql = "insert into USERS(ID,USERNAME,PASSWORD,ROLE_ID,USER_FULLNAME,DEL_FLAG) values(?,?,?,?,?,?)";
						DBBaseUtil.executeUpdate(sql, id,vo.getUsername(),vo.getPassword(),vo.getRoleId(),vo.getUserFullname(),"0");
				}else{
					
					String sql = "update USERS set USERNAME=?,PASSWORD=?,ROLE_ID=?,USER_FULLNAME=?  where id=?";
					DBBaseUtil.executeUpdate(sql,vo.getUsername(),vo.getPassword(),vo.getRoleId(),vo.getUserFullname(), vo.getId());
				}
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	public List<Roles> findRole(){
		String sql = "select * from ROLES where 1=1 ";
		List<Roles> list = DB4ObjUtil.execute4Obj(new Roles(), sql);
		
		return list;
	}
	
}
